class Access{
	public int x;
	private int y;
	int z;      // instance variable with default or package access 
	
	Access(int x, int y, int z){
		this.x = x;
		this.y = y;
		this.z =z;
	}
	
	public int getY(){
		return y;
	}
	
	public void setY(int newY){
		y = newY;
	}
}

public class AccessDriver {
        
    public static void main(String[] args) {
        Access obj = new Access(6, 8, 3);
        
        /* A public instance variable can be accessed directly from other classes in its package and
           from classes in other packages
         */
        System.out.println("x = " + obj.x);
        /* A public variable can be modified directly from other classes in its package and from classes in other packages. 
         THIS IS DANGEROUS because everyone is able to modify the variable.
         */
        obj.x= 15;
        System.out.println("x = " + obj.x);
        
        
        /* An instance variable with default or package access can be accessed directly from other classes
           in its package; but not from classes in other packages.
        */
        System.out.println("z = " + obj.z);
        /* An instance variable with default or package access can be modified directly from other classes in its
          package; but not from other packages.
        */
        obj.z = 20;
        System.out.println("z = " + obj.z);
        
        // A private instance variable cannot be accessed directly from outside its class
        // System.out.println(obj.y);     // this generates compile-time error 
        
        // The only way to access the variable is to use a get- or accessor-method:
        // This is the correct way to access a private variable:
         System.out.println("y = " + obj.getY());
         
         
         // The only way to modify a private variable is to provide a set-method (or mutator-method)
         // obj.y = 12;          // this generates compile-time error

        // This is the correct way to modify a private variable:
         obj.setY(12);
         System.out.println("y = " + obj.getY()); 
    }
}
